define([
	'layer',
	'../../Shop/module/STH_main.js',
	'selectModify',
	'imgCenter'
], function(layer, main) {

	return {
		couponAddM: function(con) {
			var _this = this,
				key = con.key,
				order = con.order,
				itemlist = con.CouponList,
				//判断营销活动是否存在

				newPanel = main.getPanel("coupon"),
				newEdit = main.getEdit("coupon"); 
			//属性操作
			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newEdit).attr("key", key);
			$(newEdit).css("display", "none");

			//延时顺序重定位
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);

			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);

			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}

			//插入编辑页
			$(".edit-panel").append(newEdit);
			g_data[key].ItemList = [];
			var listmbE = $(newEdit).find(".moban").html(),
				listmbP = $(newPanel).find(".moban").html();

				for(var g = 0; g < itemlist.length; g++) {
					var row = itemlist[g];
					var index = row["Fsdate"].indexOf('T');
					if(index > 0) row["Fsdate"] = row["Fsdate"].substr(0, index);

					var index = row["Fedate"].indexOf('T');
					if(index > 0) row["Fedate"] = row["Fedate"].substr(0, index);

					var Fsdt = row.Fsdtype == 1 ? row["Fsdate"] : "领取时间";
					var Fedt = row.Fsdtype == 1 ? row["Fedate"] : "领取后" + row.Fday + "天";
					var Fdesc = "";
					var Feditdesc = "；"
					if(row.Fctype == 1) {
						if(row.Ffullvalue == 0) {

							if(!row.Fpids) {
								Feditdesc = '全场无门槛';
								Fdesc = '全场无门槛';
							} else {
								Fdesc = '指定商品无门槛';
								Feditdesc = '指定商品无门槛消费';
							}

						} else {
							Fdesc = '满￥' + row.Ffullvalue;
							if(!row.Fpids) {
								Feditdesc = '全场满￥' + row.Ffullvalue;
							} else
								Feditdesc = '指定商品满￥' + row.Ffullvalue;
						}
					} else {
						Fdesc = '仅需￥' + row.Ffullvalue;;
					}
					var newobj = {
						"Feditdesc": Fdesc,
						"Ffacevalue": parseInt(row.Ffacevalue),
						"Fdesc": Fdesc,
						"Fctype": row.Fctype == 1 ? '立即获取' : '马上购买',
						"Fsdate": Fsdt,
						"Fedate": Fedt
					};

					//加入编辑模板
					if($(newEdit).find(".good-list-show ul li[rel='yuan']").length > 0) {
						$(newEdit).find(".good-list-show ul li[rel='yuan']").remove();
					}
					var newStrE = main.formatTemplate(newobj, listmbE);
					$(newEdit).find(".good-list-show ul").append(newStrE);

					var newobjP = newobj;
					newobjP.money_exempt = parseInt(newobjP.money_exempt);
					newobjP.money_using = parseInt(newobjP.money_using);
					newobjP.pstart_time = newobjP.start_time;
					newobjP.pend_time = newobjP.end_time;

					//加入显示模板
					if($(newPanel).find(".STH_shop_coupon .store-card-list[rel='yuan']").length > 0) {
						$(newPanel).find(".STH_shop_coupon .store-card-list[rel='yuan']").remove();
					}
					var newStrP = main.formatTemplate(newobjP, listmbP);
					$(newPanel).find(".STH_shop_coupon .store-card").append(newStrP);

				 	g_data[key].ItemList.push({
							"Ftype": 7,
							"Ftypeid": row.Fid
						});

				}
			_this.couponActive(newPanel, newEdit, key);

		},

		//得到当前优惠类型的名字
		getType_name: function(obj, val) {
			var name = "";
			for(var i = 0; i < obj.length; i++) {
				if(val == obj[i].type)
					name = obj[i].type_name;
				break;
			}
			return name;
		},

		couponActive: function(thisPanel, thisEdit, key) {
			var _this = this;

			var newOption = "<option value=''>请选择类型</option>",
				grouplen = coupons_group != "" ? coupons_group.length : undefined;
			if(grouplen) {
				for(var i = 0; i < grouplen; i++) {
					newOption += "<option value='" + coupons_group[i].type + "'>" + coupons_group[i].type_name + "</option>";
				}
			}
			$(thisEdit).find("select[name='choiceitem']").append(newOption);
			g_data[key]["typename"] = "优惠券";

			//选择类型操作
			$(thisEdit).find('select[name="choiceitem"]').selectModify({});

			//选择优惠券按钮
			$(thisEdit).find(".choicegoods").unbind("click").click(function() {

				//此处应发AJAX请求，暂用模似
				var nowtype = $(this).prev("select[name='choiceitem']").val();
				if(nowtype == "") {
					layer.msg("请选择优惠券类型");
					return;
				}
				main.commonDialog('vstore/tb_coupon/Sql.html?ctype=' + nowtype, '选择微页面', 800, 600, function(newArray) {

					var listmbE = $(thisEdit).find(".moban").html(),
						listmbP = $(thisPanel).find(".moban").html();

					for(var g = 0; g < newArray.length; g++) {
						var row = newArray[g];

						var index = row["Fsdate"].indexOf('T');
						if(index > 0) row["Fsdate"] = row["Fsdate"].substr(0, index);

						var index = row["Fedate"].indexOf('T');
						if(index > 0) row["Fedate"] = row["Fedate"].substr(0, index);

						var Fsdt = row.Fsdtype == 1 ? row["Fsdate"] : "领取时间";
						var Fedt = row.Fsdtype == 1 ? row["Fedate"] : "领取后" + row.Fday + "天";
						var Fdesc = "";
						var Feditdesc = "；"
						if(row.Fctype == 1) {
							if(row.Ffullvalue == 0) {

								if(!row.Fpids) {
									Feditdesc = '全场无门槛';
									Fdesc = '全场无门槛';
								} else {
									Fdesc = '指定商品无门槛';
									Feditdesc = '指定商品无门槛消费';
								}

							} else {
								Fdesc = '满￥' + row.Ffullvalue;
								if(!row.Fpids) {
									Feditdesc = '全场满￥' + row.Ffullvalue;
								} else
									Feditdesc = '指定商品满￥' + row.Ffullvalue;
							}
						} else {
							Fdesc = '仅需￥' + row.Ffullvalue;;
						}
						var newobj = {
							"Feditdesc": Fdesc,
							"Ffacevalue": parseInt(row.Ffacevalue),
							"Fdesc": Fdesc,
							"Fctype": row.Fctype == 1 ? '立即获取' : '马上购买',
							"Fsdate": Fsdt,
							"Fedate": Fedt
						};

						//加入编辑模板
						if($(thisEdit).find(".good-list-show ul li[rel='yuan']").length > 0) {
							$(thisEdit).find(".good-list-show ul li[rel='yuan']").remove();
						}
						var newStrE = main.formatTemplate(newobj, listmbE);
						$(thisEdit).find(".good-list-show ul").append(newStrE);

						var newobjP = newobj;

						//加入显示模板
						if($(thisPanel).find(".STH_shop_coupon .store-card-list[rel='yuan']").length > 0) {
							$(thisPanel).find(".STH_shop_coupon .store-card-list[rel='yuan']").remove();
						}
						var newStrP = main.formatTemplate(newobjP, listmbP);
						$(thisPanel).find(".STH_shop_coupon .store-card").append(newStrP);

						//编辑g_data;
						g_data[key].ItemList.push({
							"Ftype": 7,
							"Ftypeid": row.Fid
						});
					}

				});

			});

			//删除,上下移动
			function imgDelete(thisPanel, thisEdit, key) {
				$(thisEdit).undelegate(".com-list-box-delete i", 'click').delegate(".com-list-box-delete i", 'click', function() {
					var shunxu = $(thisEdit).find(".com-list-box-delete i").index(this);
					if($(thisPanel).find(".store-card .store-card-list").length <= 1) {
						$(thisPanel).find(".store-card").html($(main.getPanel("coupon")).find(".store-card").html());
					} else {
						$(thisPanel).find(".store-card .store-card-list:eq(" + shunxu + ")").remove();
					}

					if($(thisEdit).find(".good-list-show ul li").length <= 1) {
						$(thisEdit).find(".good-list-show ul").html($(main.getEdit("coupon")).find(".good-list-show ul").html());
					} else {
						$(this).parents("li").remove();
					}
					g_data[key].ItemList.splice(shunxu, 1);
				});

				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {
					if($(this).parents("ul").find("li").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-up").index(this),
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".store-card .store-card-list:eq(" + index + ")").prop("outerHTML");
					if(index > 0) {
						$(thisEdit).find(".good-list-show ul li:eq(" + (index - 1) + ")").before(htmlE);
						$(thisEdit).find(".good-list-show ul li:eq(" + (index + 1) + ")").remove();
						$(thisPanel).find(".store-card .store-card-list:eq(" + (index - 1) + ")").before(htmlP);
						$(thisPanel).find(".store-card .store-card-list:eq(" + (index + 1) + ")").remove();
						main.exchange(g_data[key].ItemList, (index - 1), index);
						console.log(g_data[key].ItemList);
					}
				});

				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					if($(this).parents("ul").find("li").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box-operate .arrow-down").length,
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".store-card .store-card-list:eq(" + index + ")").prop("outerHTML");
					if(index < (lilen - 1)) {
						$(thisEdit).find(".good-list-show ul li:eq(" + (index + 1) + ")").after(htmlE);
						$(thisEdit).find(".good-list-show ul li:eq(" + index + ")").remove();
						$(thisPanel).find(".store-card .store-card-list:eq(" + (index + 1) + ")").after(htmlP);
						$(thisPanel).find(".store-card .store-card-list:eq(" + index + ")").remove();
						main.exchange(g_data[key].ItemList, index, (index + 1));

					}
				});
			}

			imgDelete(thisPanel, thisEdit, key);
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}
	}
});